package hu.tortavar2.client.view.adminpages;

import hu.tortavar2.client.AdminService;
import hu.tortavar2.client.AdminServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class AdminMainPage_AddProduct extends Composite {

	AdminServiceAsync adminService = GWT.create(AdminService.class);
	
	private static AdminMainPage_AddProductUiBinder uiBinder = GWT
			.create(AdminMainPage_AddProductUiBinder.class);

	interface AdminMainPage_AddProductUiBinder extends
			UiBinder<Widget, AdminMainPage_AddProduct> {
	}

	public AdminMainPage_AddProduct() {
		initWidget(uiBinder.createAndBindUi(this));
		setFields();
	}

	@UiField Anchor addNewProductButton;
	@UiField Grid table;
	@UiField FormPanel addNewProductForm;
	@UiField HTMLPanel grid_02_left;
	//@UiField HTMLPanel grid_02_right;
	@UiField HTMLPanel padding_color_left;
	//@UiField HTMLPanel padding_color_right;
	@UiField ListBox productCategoryListBox;
	
	private void setFields() {
		addNewProductButton.getElement().setClassName("button");
		addNewProductForm.setWidget(table);
		addNewProductButton.setName("addNewProductForm");
		addNewProductForm.setMethod(FormPanel.METHOD_POST);
		addNewProductForm.setEncoding(FormPanel.ENCODING_MULTIPART);
		setBlobStoreUrl();
//		addNewProductForm.setAction(GWT.getModuleBaseURL() + "uploadservlet");
		grid_02_left.getElement().setClassName("grid_02");
//		grid_02_right.getElement().setClassName("grid_02");
		padding_color_left.getElement().setClassName("padding pink");
//		padding_color_right.getElement().setClassName("padding pink");
	}

	private void setBlobStoreUrl() {
		adminService.getBlobStoreUploadUrl(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				addNewProductForm.setAction(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	@UiHandler("addNewProductButton")
	void onclickSubmit(ClickEvent e ) {
		addNewProductForm.submit();
		addNewProductForm.reset();
	}
}
